package com.java.simple.study.juc.pattern;

import com.java.simple.study.tool.Sleeper;

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
 * @author zhoujunlin
 * @date 2022年11月07日 22:09
 * @desc
 */
public class TestBalking {

    private static final String FILE_NAME = "D:\\mylog.log";
    private static final ConcurrentHashMap<String, String> INFO = new ConcurrentHashMap<>();

    public static void main(String[] args) {
        FileSaver fileSaver = new FileSaver(FILE_NAME);
        ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(1);
        scheduledExecutorService.scheduleAtFixedRate(() -> {
            fileSaver.save(INFO);
        }, 0, 2, TimeUnit.SECONDS);

        Sleeper.sleep(1);
        new Thread(() -> {
            INFO.put("aaa", "bbb");
            fileSaver.change();
        }).start();

        Sleeper.sleep(2);
        new Thread(() -> {
            INFO.put("ccc", "ddd");
            fileSaver.change();
        }).start();
    }

}
